import* as userServices from '../../services/user'
import {message} from 'antd';
import {sortBy} from '../../utils/sortArray'
import {getcookie} from '../../utils/cookies';
export default {
  namespace: 'user',
  state: {
    menu: [
    {
      pri: 1,
      id: 1001,
      menuName: '热门课程',
      subMenuName: [
        {pri: 1, id: 1001, name: '彭耀凯的成才史'},
        {pri: 2, id: 1002, name: '黄蓥的压马路历程'},
        {pri: 3, id: 1003, name: '飞哥教你做鱼沫沫'},
        {pri: 4, id: 1004, name: '学姐教你找东西'}]
    },    {
      pri: 2,
      id: 1002,
      menuName: '我的课程',
      subMenuName: []
    },
  ],
    cookieToken: null,//cookieToken用来标记用户登录，用户展示我的课程
    classInfo:[],
    oneClassInfo:{}

  },
  reducers: {
    //保存侧边栏需要用的数据，分组和系列
    saveMenuInfo(state, {payload:newData}){
      return {
        ...state,
        menu: newData
      }
    },
    //点击下拉框，在最初获取的全分类列表中获取二级菜单
    saveOneGroupInfo(state, {payload}){
      console.log(payload);
      //获取所有的系列课程ID
      return {
        ...state,
        classInfo:payload,
      }
    },
//存放视频的播放链接
    saveOneClassInfo(state, {payload}){
      console.log(payload);
      return {
        ...state,
        oneClassInfo:payload,
      }
    },
  },
  effects: {
    //一开始就获取菜单栏
    *fetchMenu({payload:cookieToken}, {call, put}){
      //侧边栏点击 或点击顶部第二个导航拿到初始组别列表
      let {data} = yield call(userServices.fetchMenu);
      console.log('获取到的全部数据列表', data);
      let newData;
      let allClassId =[];
      if (data.length == 0) {
        newData = [{pri: 1, menuName: '课程待添加', subMenuName: [{pri: 1, id: 1001, name: '系列待添加'}]}]
      } else {
        newData = data.map((item) => {
          item.id = item.courseId;
          delete item.courseId;
          item.pri = item.coursePri;
          delete item.coursePri;
          item.menuName = item.courseName;
          delete item.courseName;
          item.subMenuName = item.groupClassList.map((child) => {
            child.id = child.courseClass.classId;
            child.name = child.courseClass.className;
            delete child.courseClass;
            allClassId.push(child.id);
            return child
          });
          item.subMenuName.sort(sortBy('pri', false, parseInt));
          delete item.groupClassList;
          return item
        });
      }
      newData.sort(sortBy('pri', false, parseInt));
      console.log('改变过键名并排序后的数据', newData);
      //增加一个TAB用于展示自己的课程
      newData.push({id: newData[newData.length -1 ].id + 1,pri:newData[newData.length -1 ].pri + 1,menuName:'我的课程'});
      if(cookieToken){
        //如果登录成功，获取到cookieToken的话就去获取我的课程
        let unionid = getcookie('unionid');
        yield put({type: "fetchMyClass", payload: unionid});
      }else{
        //否则获取默认的热门课程
        yield put({type: "fetchOneGroupInfo", payload: newData[0].id});
      }
      yield put({type: "saveMenuInfo", payload: newData});

    },
    //获取一个tab有关的所有课程
    *fetchOneGroupInfo({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(userServices.fetchOneGroupInfo,values);
      console.log('获取到的一个组别的所有系列列表', data);
      let newData = data.map((item) => {
        item.name = item.courseClass.className;
        item.classPicName = item.courseClass.classPicName;
        // delete item.courseClass;
        delete item.course;
        return item
      });

      newData.sort(sortBy('pri', false, parseInt));
      console.log(newData)
      yield put({type: "saveOneGroupInfo", payload: newData})
    },
    //获取一个课程的链接
    *fetchLiveVideo({payload:values}, {call, put}){
      console.log(values);
      // let {data} = yield call(userServices.fetchLiveVideo,values);
      // console.log('临时数据', data);
      // let newData ={ link:'http://www.w3school.com.cn/i/movie.mp4',type:'mp4'};
      let newData;
      newData ={ link:'http://zhengzhi.huanqiushuju.com/zhengzhi/zhengzhi.flv',type:'flv'};
      // if(values == 1001){
      //   newData ={ link:'http://zhengzhi.huanqiushuju.com/zhengzhi/zhengzhi.flv',type:'flv'};
      // }else if(values == 1002){
      //   newData ={ link:'http://zhengzhi.huanqiushuju.com/zhengzhi/zhengzhi.flv',type:'flv'};
      // }else if(values == 1003){
      //   newData ={ link:'http://localhost:7001/live/test.flv',type:'flv'};
      // }

      yield put({type: "saveOneClassInfo", payload: newData});
    },

    //获取用户个人课程的列表
    //获取一个tab有关的所有课程
    *fetchMyClass({payload:unionid}, {call, put}){
      console.log(unionid);
      let {data} = yield call(userServices.fetchMyClass,unionid);
      console.log('获取到用户的个人课程列表', data);
      let newData = data.map((item) => {
        item.name = item.courseClass.className;
        item.classPicName = item.courseClass.classPicName;
        delete item.course;
        return item
      });
      newData.sort(sortBy('pri', false, parseInt));
      console.log(newData);
      yield put({type: "saveOneGroupInfo", payload: newData})
    },
  },
  subscriptions: {
    //监听到video地址下时,获取相应的组别、系列
    setup({dispatch, history}){
      return history.listen(({pathname, query}) => {
        console.log(query);
        // if (pathname === '/class') {
        //   let cookieToken = getcookie('cookieToken');
        //   dispatch({type: 'fetchMenu', payload: cookieToken})
        // }
      })
    }
  },
};
